package ejb.contentManagement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ejb.dto.NationDTO;
import entities.Nation;

@Stateless
public class NationBean implements NationMgr {
	
	@PersistenceContext
    private EntityManager em;
	
	/**
	 * Retrieve entity Nation from the associated Id
	 * @param id
	 * @return entity Nation associated to the <code>id</code>
	 */
	public Nation getNation(int id) {
		return em.find(Nation.class, id);
	}
	
	/**
	 * Retrieve all Nation entities
	 * @return all Nation entities
	 */
	public List<Nation> getAllNations() {
    	return em.createNamedQuery("Nation.findAll", Nation.class)
                .getResultList();
    }
	
	/**
	 * Retrieve entity Nation from the associated Id and convert it to corresponding NationDTO
	 */
	@Override
	public NationDTO getNationDTO(int id) {
		return  convertToDTO(getNation(id));
    }
	
	/**
	 * Retrieve all Nation entities and convert them into corresponding list of NationDTO
	 */
	@Override
	public List<NationDTO> getAllNationsDTO() {
		List<Nation> list = getAllNations();
		List<NationDTO> newList = new ArrayList<NationDTO>();
		for(Nation obj : list) {
			newList.add(convertToDTO(obj));
		}
		return newList;
	}
	
	/**
	 * Convert Nation entity (parameter <code>n</code>) into corresponding NationDTO
	 * @param n
	 * @return converted NationDTO
	 */
	static NationDTO convertToDTO(Nation n) {
		if(n == null){
			return null;
		}
		NationDTO dto = new NationDTO();
		dto.setId(n.getId());
		dto.setName(n.getName());
		return dto;
	}
	
	/**
	 * Retrieve entity Nation from the associated Id and delete the corresponding element from the database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean deleteNation(int id) {
		try {
			em.remove(em.find(Nation.class, id));
			em.flush();
		}
		//Catch exceptions in delete operation from database
		catch(Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * Create entity Nation from corresponding NationDTO and update corresponding element in database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean updateNation(NationDTO updatedNation) {
		if(updatedNation.getName().equals("") ||
			updatedNation.getName().startsWith(" ") || 
			updatedNation.getName()==null)
			return false;
		try{
			em.merge(new Nation(updatedNation));
			em.flush();
		}
		//Catch exceptions in the update operation of database
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	/**
	 * Create entity Nation from corresponding NationDTO and insert new corresponding element in database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean insertNation(NationDTO newNation) {
		if(newNation.getName().equals("") || newNation.getName().startsWith(" ") || newNation.getName()==null) {
			return false;
		}
		try {
			em.persist(new Nation(newNation));
			em.flush();
		}
		//Catch exceptions in the insert operation in database
		catch(Exception e) {
			return false;
		}
		return true;
	}
	
}
